//
//  XYTLoginRegisterViewController.m
//  XYTPayOrderSystem
//
//  Created by wupeng on 2017/8/23.
//  Copyright © 2017年 wupeng. All rights reserved.
//

#import "XYTLoginRegisterViewController.h"
#import "XYTLoginModel.h"
#import "XYTRegisterViewController.h"

@interface XYTLoginRegisterViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *userNameField;

@property (nonatomic, strong) UITextField *passwordField;

@end

@implementation XYTLoginRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户登录";
    
    [self setupUI];
    
}

- (void)setupUI{
    UILabel *userNameLab = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, 100, 18)];
    userNameLab.textColor = [UIColor colorWithRed:69.0f/255.0 green:140.0f/255.0 blue:200.0f/255.0 alpha:1];
    userNameLab.text = @"用户名:";
    userNameLab.font = [UIFont systemFontOfSize:13.0f];
    [self.view addSubview:userNameLab];
    
    UITextField *userNameField = [[UITextField alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(userNameLab.frame) + 5, kScreen_Width - 30*2, 40)];
    userNameField.borderStyle = UITextBorderStyleRoundedRect;
    userNameField.font = [UIFont systemFontOfSize:16.0f];
    userNameField.textAlignment = NSTextAlignmentLeft;
    userNameField.keyboardType = UIKeyboardTypeNumberPad;
    userNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
    userNameField.delegate = self;
    userNameField.placeholder = @"请输入用户名";
    [self.view addSubview:userNameField];
    self.userNameField = userNameField;
    
    UILabel *passwordLab = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(userNameField.frame) + 15, 100, 18)];
    passwordLab.textColor = [UIColor colorWithRed:69.0f/255.0 green:140.0f/255.0 blue:200.0f/255.0 alpha:1];
    passwordLab.text = @"密码:";
    passwordLab.font = [UIFont systemFontOfSize:13.0f];
    [self.view addSubview:passwordLab];
    
    UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(passwordLab.frame) + 8, kScreen_Width - 30*2, 40)];
    passwordField.borderStyle = UITextBorderStyleRoundedRect;
    passwordField.font = [UIFont systemFontOfSize:16.0f];
    passwordField.placeholder = @"请输入密码";
    passwordField.secureTextEntry = YES;
    passwordField.textAlignment = NSTextAlignmentLeft;
    passwordField.keyboardType = UIKeyboardTypeNumberPad;
    passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
    passwordField.delegate = self;
    [self.view addSubview:passwordField];
    self.passwordField = passwordField;
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(30, CGRectGetMaxY(passwordField.frame) + 30, kScreen_Width - 30*2, 40);
    loginBtn.backgroundColor = [UIColor colorWithRed:69.0f/255.0 green:140.0f/255.0 blue:200.0f/255.0 alpha:1];
    [loginBtn.layer setCornerRadius:2.0];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(submitLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    
    UIButton *registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    registerBtn.frame = CGRectMake(30, CGRectGetMaxY(loginBtn.frame) + 20, kScreen_Width - 30*2, 40);
    registerBtn.backgroundColor = [UIColor colorWithRed:69.0f/255.0 green:140.0f/255.0 blue:200.0f/255.0 alpha:1];
    [registerBtn.layer setCornerRadius:2.0];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(gotoRegisterPage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
    
}
//登录请求
- (void)submitLogin{
    [UIView showHUD:nil];
    NSString *userName = self.userNameField.text;
    NSString *password = self.passwordField.text;
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@%@",AppKey,AppSecret,userName,password];
    NSString *md5Sign = [Md5Util md5:signStr];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params xyt_setObj:userName forKey:@"phone"];
    [params xyt_setObj:password forKey:@"password"];
    [params xyt_setObj:md5Sign forKey:@"sign"];
    XYTWeakSelf(self)
    [XYTNetRequest POST:LoginUrl parameters:params isNeedKey:NO success:^(id responseObject) {
        [UIView hideHUD];
        [UIView showCustomToast:@"登录成功"];
        XYTStrongSelf(weakSelf)
        XYTLoginModel *loginInfo = [XYTLoginModel mj_objectWithKeyValues:responseObject];
        [XYTUserInfoManager sharedManager].token = loginInfo.token;
        [XYTUserInfoManager sharedManager].phone = loginInfo.phone;
        [XYTUserInfoManager sharedManager].roleType = loginInfo.roleType;
        [[XYTUserInfoManager sharedManager] saveUserInfo];
        if (strongSelf.navigationController.viewControllers.count>1) {
            [strongSelf.navigationController popViewControllerAnimated:YES];
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:Login_Success object:nil];
        
    } failure:^(NSString *errorMsg) {
        XYTStrongSelf(weakSelf)
        [UIView hideHUD];
        [strongSelf showAlertWithTitle:errorMsg];
    }];
}

//注册请求
- (void)gotoRegisterPage{
    XYTRegisterViewController *registerVC = [[XYTRegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.userNameField resignFirstResponder];
    [self.passwordField resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
